/*
 * CadastrarRepre.java
 *
 * Created on 02/10/2009, 09:24:54
 * Copyright LAS
 */
package Soligee.view;

import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeBuscarImplements;
import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeCadastrarImplements;
import Soligee.decorador.ValidaCamposObrigatorios;
import Soligee.model.vo.cadastros.CidadeVO;
import Soligee.model.vo.cadastros.RepresentanteVO;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlCommandLink;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.ext.HtmlPanelGroup;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectedData;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectionItems;
import com.icesoft.faces.component.jsfcl.data.PopupBean;
import com.icesoft.faces.component.jsfcl.data.SelectInputDateBean;
import com.icesoft.faces.component.panelpopup.PanelPopup;
import com.icesoft.faces.component.paneltabset.PanelTab;
import com.icesoft.faces.component.paneltabset.PanelTabSet;
import com.icesoft.faces.component.selectinputdate.SelectInputDate;
import com.sun.rave.faces.converter.SqlDateConverter;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import javax.faces.FacesException;
import java.util.Date;
import java.util.List;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;


/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class CadastrarRepre extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    private int __placeholder;
    private int controle =0;
    //variavel para controle de background de campos obrigatorios
    private ValidaCamposObrigatorios valida = new ValidaCamposObrigatorios();
    
    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private SelectInputDateBean selectInputDate1Bean = new SelectInputDateBean();

    public SelectInputDateBean getSelectInputDate1Bean() {
        return selectInputDate1Bean;
    }

    public void setSelectInputDate1Bean(SelectInputDateBean sidb) {
        this.selectInputDate1Bean = sidb;
    }
    //Tipo Endereço
    private DefaultSelectedData botaoSelecionadoTipoEndereco = new DefaultSelectedData();

    public DefaultSelectedData getBotaoSelecionadoTipoEndereco() {
        return botaoSelecionadoTipoEndereco;
    }

    public void setBotaoSelecionadoTipoEndereco(DefaultSelectedData dsd) {
        this.botaoSelecionadoTipoEndereco = dsd;
    }
    private DefaultSelectionItems botaoTipoEndereco = new DefaultSelectionItems();

    public DefaultSelectionItems getBotaoTipoEndereco() {
        return botaoTipoEndereco;
    }

    public void setBotaoTipoEndereco(DefaultSelectionItems dsi) {
        this.botaoTipoEndereco = dsi;
    }

    //Opção Uf
    private DefaultSelectedData botaoSelecionadoOpcaoUf = new DefaultSelectedData();

    public DefaultSelectedData getBotaoSelecionadoOpcaoUf() {
        return botaoSelecionadoOpcaoUf;
    }

    public void setBotaoSelecionadoOpcaoUf(DefaultSelectedData dsd) {
        this.botaoSelecionadoOpcaoUf = dsd;
    }
    private DefaultSelectionItems botaoOpcaoUf = new DefaultSelectionItems();

    public DefaultSelectionItems getBotaoOpcaoUf() {
        return botaoOpcaoUf;
    }

    public void setBotaoOpcaoUf(DefaultSelectionItems dsi) {
        this.botaoOpcaoUf = dsi;
    }

    //OpcaoCidade
    private DefaultSelectedData botaoSelecionadoOpcaoCidade = new DefaultSelectedData();

    public DefaultSelectedData getBotaoSelecionadoOpcaoCidade() {
        return botaoSelecionadoOpcaoCidade;
    }

    public void setBotaoSelecionadoOpcaoCidade(DefaultSelectedData dsd) {
        this.botaoSelecionadoOpcaoCidade = dsd;
    }
    private DefaultSelectionItems botaoOpcaoCidade = new DefaultSelectionItems();

    public DefaultSelectionItems getBotaoOpcaoCidade() {
        return botaoOpcaoCidade;
    }

    public void setBotaoOpcaoCidade(DefaultSelectionItems dsi) {
        this.botaoOpcaoCidade = dsi;
    }
    private HtmlSelectOneMenu opcaoCidade = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getOpcaoCidade() {
        return opcaoCidade;
    }

    public void setOpcaoCidade(HtmlSelectOneMenu hsom) {
        this.opcaoCidade = hsom;
    }
    private HtmlPanelGroup painelPrincipal = new HtmlPanelGroup();

    public HtmlPanelGroup getPainelPrincipal() {
        return painelPrincipal;
    }

    public void setPainelPrincipal(HtmlPanelGroup hpg) {
        this.painelPrincipal = hpg;
    }
    private HtmlInputText campoNome = new HtmlInputText();

    public HtmlInputText getCampoNome() {
        return campoNome;
    }

    public void setCampoNome(HtmlInputText hit) {
        this.campoNome = hit;
    }
    private SelectInputDate dataCadastro = new SelectInputDate();

    public SelectInputDate getDataCadastro() {
        return dataCadastro;
    }

    public void setDataCadastro(SelectInputDate sid) {
        this.dataCadastro = sid;
    }
    private HtmlInputText campoTelefoneCelular = new HtmlInputText();

    public HtmlInputText getCampoTelefoneCelular() {
        return campoTelefoneCelular;
    }

    public void setCampoTelefoneCelular(HtmlInputText hit) {
        this.campoTelefoneCelular = hit;
    }
    private HtmlInputText campoEmail = new HtmlInputText();

    public HtmlInputText getCampoEmail() {
        return campoEmail;
    }

    public void setCampoEmail(HtmlInputText hit) {
        this.campoEmail = hit;
    }
    private HtmlInputText campoTelefoneComercial = new HtmlInputText();

    public HtmlInputText getCampoTelefoneComercial() {
        return campoTelefoneComercial;
    }

    public void setCampoTelefoneComercial(HtmlInputText hit) {
        this.campoTelefoneComercial = hit;
    }
    private HtmlInputText campoPaginaWeb = new HtmlInputText();

    public HtmlInputText getCampoPaginaWeb() {
        return campoPaginaWeb;
    }

    public void setCampoPaginaWeb(HtmlInputText hit) {
        this.campoPaginaWeb = hit;
    }
    private HtmlCommandLink link_proximoPassoAba01 = new HtmlCommandLink();

    public HtmlCommandLink getLink_proximoPassoAba01() {
        return link_proximoPassoAba01;
    }

    public void setLink_proximoPassoAba01(HtmlCommandLink hcl) {
        this.link_proximoPassoAba01 = hcl;
    }
    private HtmlCommandLink linkCancelarAba02 = new HtmlCommandLink();

    public HtmlCommandLink getLinkCancelarAba02() {
        return linkCancelarAba02;
    }

    public void setLinkCancelarAba02(HtmlCommandLink hcl) {
        this.linkCancelarAba02 = hcl;
    }
    private HtmlInputText campoRua = new HtmlInputText();

    public HtmlInputText getCampoRua() {
        return campoRua;
    }

    public void setCampoRua(HtmlInputText hit) {
        this.campoRua = hit;
    }
    private HtmlInputText campoNumero = new HtmlInputText();

    public HtmlInputText getCampoNumero() {
        return campoNumero;
    }

    public void setCampoNumero(HtmlInputText hit) {
        this.campoNumero = hit;
    }
    private HtmlInputText campoComplemento = new HtmlInputText();

    public HtmlInputText getCampoComplemento() {
        return campoComplemento;
    }

    public void setCampoComplemento(HtmlInputText hit) {
        this.campoComplemento = hit;
    }
    private HtmlInputText campoBairro = new HtmlInputText();

    public HtmlInputText getCampoBairro() {
        return campoBairro;
    }

    public void setCampoBairro(HtmlInputText hit) {
        this.campoBairro = hit;
    }
    private HtmlInputText campoCep = new HtmlInputText();

    public HtmlInputText getCampoCep() {
        return campoCep;
    }

    public void setCampoCep(HtmlInputText hit) {
        this.campoCep = hit;
    }
    private HtmlSelectOneMenu opcaoUf = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getOpcaoUf() {
        return opcaoUf;
    }

    public void setOpcaoUf(HtmlSelectOneMenu hsom) {
        this.opcaoUf = hsom;
    }
    private HtmlCommandButton btn_cadastrar1 = new HtmlCommandButton();

    public HtmlCommandButton getBtn_cadastrar1() {
        return btn_cadastrar1;
    }

    public void setBtn_cadastrar1(HtmlCommandButton hcb) {
        this.btn_cadastrar1 = hcb;
    }
    private HtmlInputText campoCodigo = new HtmlInputText();

    public HtmlInputText getCampoCodigo() {
        return campoCodigo;
    }

    public void setCampoCodigo(HtmlInputText hit) {
        this.campoCodigo = hit;
    }
    private PanelTabSet painelAbasRepresentante = new PanelTabSet();

    public PanelTabSet getPainelAbasRepresentante() {
        return painelAbasRepresentante;
    }

    public void setPainelAbasRepresentante(PanelTabSet pts) {
        this.painelAbasRepresentante = pts;
    }
    private HtmlCommandLink linkCancelarAba01 = new HtmlCommandLink();

    public HtmlCommandLink getLinkCancelarAba01() {
        return linkCancelarAba01;
    }

    public void setLinkCancelarAba01(HtmlCommandLink hcl) {
        this.linkCancelarAba01 = hcl;
    }
    private PopupBean panelPopup1Bean = new PopupBean();

    public PopupBean getPanelPopup1Bean() {
        return panelPopup1Bean;
    }

    public void setPanelPopup1Bean(PopupBean pb) {
        this.panelPopup1Bean = pb;
    }
    private PanelPopup painelPopUpAviso = new PanelPopup();

    public PanelPopup getPainelPopUpAviso() {
        return painelPopUpAviso;
    }

    public void setPainelPopUpAviso(PanelPopup pp) {
        this.painelPopUpAviso = pp;
    }
    private HtmlOutputText textoPopUpAviso = new HtmlOutputText();

    public HtmlOutputText getTextoPopUpAviso() {
        return textoPopUpAviso;
    }

    public void setTextoPopUpAviso(HtmlOutputText hot) {
        this.textoPopUpAviso = hot;
    }
    private HtmlCommandButton button1 = new HtmlCommandButton();

    public HtmlCommandButton getButton1() {
        return button1;
    }

    public void setButton1(HtmlCommandButton hcb) {
        this.button1 = hcb;
    }
    private HtmlCommandButton btn_popUpAviso2 = new HtmlCommandButton();

    public HtmlCommandButton getBtn_popUpAviso2() {
        return btn_popUpAviso2;
    }

    public void setBtn_popUpAviso2(HtmlCommandButton hcb) {
        this.btn_popUpAviso2 = hcb;
    }
    private HtmlCommandButton btn_popUpAviso1 = new HtmlCommandButton();

    public HtmlCommandButton getBtn_popUpAviso1() {
        return btn_popUpAviso1;
    }

    public void setBtn_popUpAviso1(HtmlCommandButton hcb) {
        this.btn_popUpAviso1 = hcb;
    }
    private PanelTab aba01 = new PanelTab();

    public PanelTab getAba01() {
        return aba01;
    }

    public void setAba01(PanelTab pt) {
        this.aba01 = pt;
    }
    private PanelTab aba02 = new PanelTab();

    public PanelTab getAba02() {
        return aba02;
    }

    public void setAba02(PanelTab pt) {
        this.aba02 = pt;
    }
    private PopupBean panelPopup1Bean1 = new PopupBean();

    public PopupBean getPanelPopup1Bean1() {
        return panelPopup1Bean1;
    }

    public void setPanelPopup1Bean1(PopupBean pb) {
        this.panelPopup1Bean1 = pb;
    }
    private PanelPopup panelPopup = new PanelPopup();

    public PanelPopup getPanelPopup() {
        return panelPopup;
    }

    public void setPanelPopup(PanelPopup pp) {
        this.panelPopup = pp;
    }
    private HtmlOutputText textoPopUp = new HtmlOutputText();

    public HtmlOutputText getTextoPopUp() {
        return textoPopUp;
    }

    public void setTextoPopUp(HtmlOutputText hot) {
        this.textoPopUp = hot;
    }
    private HtmlCommandButton btnPopUp1 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp1() {
        return btnPopUp1;
    }

    public void setBtnPopUp1(HtmlCommandButton hcb) {
        this.btnPopUp1 = hcb;
    }
    private HtmlCommandButton btnPopUp2 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp2() {
        return btnPopUp2;
    }

    public void setBtnPopUp2(HtmlCommandButton hcb) {
        this.btnPopUp2 = hcb;
    }
    private SqlDateConverter sqlDateConverter1 = new SqlDateConverter();

    public SqlDateConverter getSqlDateConverter1() {
        return sqlDateConverter1;
    }

    public void setSqlDateConverter1(SqlDateConverter sdc) {
        this.sqlDateConverter1 = sdc;
    }

    // </editor-fold>

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public CadastrarRepre() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        inicializaCampos();
       
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
            
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("CadastrarRepre Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected SessionBean1 getview$SessionBean1() {
        return (SessionBean1) getBean("view$SessionBean1");
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected ApplicationBean1 getview$ApplicationBean1() {
        return (ApplicationBean1) getBean("view$ApplicationBean1");
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected RequestBean1 getview$RequestBean1() {
        return (RequestBean1) getBean("view$RequestBean1");
    }

    //Link - Próximo Passo
    public String link_proximoPassoAba01_action() {
            valida.verificaCampoVazio(campoNome);
            aba01.setDisabled(true);
            aba02.setDisabled(false);
            painelAbasRepresentante.setSelectedIndex(1);
            
        return null;
    }

    //Método para LimparCampos
    public void limpaCampos(){
        campoNome.resetValue();
        campoTelefoneCelular.resetValue();
        dataCadastro.resetValue();
        campoTelefoneComercial.resetValue();
        campoEmail.resetValue();
        campoPaginaWeb.resetValue();
        campoRua.resetValue();
        campoNumero.resetValue();
        campoComplemento.resetValue();
        campoBairro.resetValue();
        campoCep.resetValue();
        opcaoUf.resetValue();
        painelAbasRepresentante.setSelectedIndex(0);
    }
    
     public void inicializaCampos(){
         //Inicializar Aba00
        painelAbasRepresentante.setSelectedIndex(0);
        painelPopUpAviso.setRendered(false);


        //Inicializa Endereço
        botaoTipoEndereco.clear();
        botaoTipoEndereco.add(new SelectItem(1,"Celular"));
        botaoTipoEndereco.add(new SelectItem(2,"Comercial"));

        //Inicializa Estado
        botaoOpcaoUf.clear();
        botaoOpcaoUf.add(new SelectItem(1,"AC"));
        botaoOpcaoUf.add(new SelectItem(2,"AL"));
        botaoOpcaoUf.add(new SelectItem(3,"AP"));
        botaoOpcaoUf.add(new SelectItem(4,"AM"));
        botaoOpcaoUf.add(new SelectItem(5,"BA"));
        botaoOpcaoUf.add(new SelectItem(6,"CE"));
        botaoOpcaoUf.add(new SelectItem(7,"DF"));
        botaoOpcaoUf.add(new SelectItem(8,"ES"));
        botaoOpcaoUf.add(new SelectItem(9,"GO"));
        botaoOpcaoUf.add(new SelectItem(10,"MA"));
        botaoOpcaoUf.add(new SelectItem(11,"MT"));
        botaoOpcaoUf.add(new SelectItem(12,"MS"));
        botaoOpcaoUf.add(new SelectItem(13,"MG"));
        botaoOpcaoUf.add(new SelectItem(14,"PA"));
        botaoOpcaoUf.add(new SelectItem(15,"PB"));
        botaoOpcaoUf.add(new SelectItem(16,"PR"));
        botaoOpcaoUf.add(new SelectItem(17,"PE"));
        botaoOpcaoUf.add(new SelectItem(18,"PI"));
        botaoOpcaoUf.add(new SelectItem(19,"RR"));
        botaoOpcaoUf.add(new SelectItem(20,"RO"));
        botaoOpcaoUf.add(new SelectItem(21,"RJ"));
        botaoOpcaoUf.add(new SelectItem(22,"RN"));
        botaoOpcaoUf.add(new SelectItem(23,"RS"));
        botaoOpcaoUf.add(new SelectItem(24,"SC"));
        botaoOpcaoUf.add(new SelectItem(25,"SP"));
        botaoOpcaoUf.add(new SelectItem(26,"SE"));
        botaoOpcaoUf.add(new SelectItem(27,"TO"));

        //Inicializa Cidades
        botaoOpcaoCidade.clear();


       //Inicializa Código Representante
        SessionFacadeBuscarImplements facadeBusca= new SessionFacadeBuscarImplements();
        campoCodigo.setValue(facadeBusca.totalDeRegistros(RepresentanteVO.class)+1);

        aba01.setDisabled(false);
        aba02.setDisabled(true);

        //Inicializa-PopUp
        panelPopup.setRendered(false);
        //inicializando controle de navegação
        limpaCampos();
        
     }

    //Método de Inicialização das Cidades
     public DefaultSelectionItems InicializaCampoCidade(int codEstado){
          SessionFacadeBuscarImplements facadeBusca= new SessionFacadeBuscarImplements();
          DefaultSelectionItems itens = new DefaultSelectionItems();
          itens.clear();
          try{
              //String campo_desejado, String oque_buscar,String nomeObjetoAssociado,Class classe
              List <CidadeVO> listaCidades= facadeBusca.buscaTodosComAssociacao("codEstado",codEstado,"codEstado","codCidade",CidadeVO.class);
                     int j=0;
                        while(j< listaCidades.size())
                    {
                        itens.add(new SelectItem( listaCidades.get(j).getCodCidade(), listaCidades.get(j).getNome()));
                        j++;
                    }
                     this.setBotaoOpcaoCidade(itens);
          }catch(NullPointerException e){
              System.err.print("Erro em inicializar o combobox:"+e.getMessage());
          }
          return null;
      }
      
    // OpcaoEstado - ProcessValueChange
    public void opcaoUf_processValueChange(ValueChangeEvent vce) {
        opcaoCidade.setDisabled(false);
        InicializaCampoCidade(Integer.parseInt(opcaoUf.getValue().toString()));
    }

    //Link Cancelar Aba01
    public String linkCancelarAba01_action() {
        this.limpaCampos();
        aba01.setDisabled(false);
        aba02.setDisabled(true);
        opcaoCidade.setDisabled(true);
        return null;
    }

    //Link Cancelar Aba02
    public String linkCancelarAba02_action() {
           this.limpaCampos();
           aba01.setDisabled(false);
           aba02.setDisabled(true);
        return null;
    }

    //Btn Cadastrar
    public String btn_cadastrar1_action()throws Exception{
            valida.verificaCampoVazio(campoNome);
            valida.verificaCampoVazio(campoRua);
            valida.verificaCampoVazio(campoNumero);
            valida.verificaCampoVazio(campoBairro);
            valida.verificaCampoVazio(campoCep);
            
            if(valida.verificaCampoVazio(campoNome)){
                controlaPopup(true,"Campos Obrigatório!",false,true,"","Ok");
                painelAbasRepresentante.setSelectedIndex(0);
            }
            else if(valida.verificaCampoVazio(campoRua)||valida.verificaCampoVazio(campoNumero)||valida.verificaCampoVazio(campoBairro)||valida.verificaCampoVazio(campoCep))
                controlaPopup(true,"Campos Obrigatórios!",false,true,"","Não");

            else
            controlaPopup(true,"Confirma Cadastro de Representante ?",true,true,"Sim","Não");
            
        return null;
    }

    public String btn_voltar_action() {
        controle = 1;
        controlaPopup(true,"Deseja Sair ?",true,true,"Sim","Não");        
        return null;
    }

     public void controlaPopup(boolean renderedPopup,String textoPoup,boolean btn1Rendered, boolean btn2Rendered,
            String textobtn1, String textobtn2){
        panelPopup.setRendered(renderedPopup);
        textoPopUp.setValue(textoPoup);
        btnPopUp1.setValue(textobtn1);
        btnPopUp1.setRendered(btn1Rendered);
        btnPopUp2.setRendered(btn2Rendered);
        btnPopUp2.setValue(textobtn2);
    }
    
    public String commandLink1_action() {
        valida.verificaCampoVazio(campoRua);
        valida.verificaCampoVazio(campoNumero);
        valida.verificaCampoVazio(campoBairro);
        valida.verificaCampoVazio(campoCep);
        aba01.setDisabled(false);
        aba02.setDisabled(true);
        painelAbasRepresentante.setSelectedIndex(0);
        return null;
    }

    public String btn_popUpAviso2_action() {
        painelPopUpAviso.setRendered(false);
        btn_popUpAviso2.setRendered(true);
        btn_popUpAviso1.setRendered(true);
        return null;
    }

    public String panelLayoutLogo_action() {
        //return null means stay on the same page
        return "voltar";
    }

    public String btnPopUp1_action() {
        if(controle==0){
        SessionFacadeCadastrarImplements cadastrar = new SessionFacadeCadastrarImplements();
        Date datacadastro = (Date) (Object) dataCadastro.getValue();
       try{

            if(cadastrar.cadastrarRepresentante(Integer.parseInt(campoCodigo.getValue().toString()), campoNome.getValue().toString(),
            campoTelefoneCelular.getValue().toString(), campoTelefoneComercial.getValue().toString(),
            campoEmail.getValue().toString(), campoPaginaWeb.getValue().toString(), "", datacadastro, 1,campoRua.getValue().toString(),
            campoBairro.getValue().toString(), campoCep.getValue().toString(), campoNumero.getValue().toString(), campoComplemento.getValue().toString(),Integer.parseInt(opcaoCidade.getValue().toString()))){
                controlaPopup(true,"Representante Cadastrado com Sucesso!",false,true,"","OK");
                inicializaCampos();
            }
         }//fim do try
         catch (Exception e){
            painelPopUpAviso.setRendered(true);
            textoPopUpAviso.setValue("Erro:" + e.getMessage() + "caused by: " + e.getStackTrace());
            btn_popUpAviso2.setValue("OK");
            btn_popUpAviso1.setRendered(false);
            limpaCampos();

            System.err.println(e.getMessage());
        }
        return null;
        }
        if(controle==1){
            panelPopup.setRendered(false);
            System.out.println("passou aki");
            return "voltar";
        }
        return null;
    }

    public String btnPopUp2_action() {
        panelPopup.setRendered(false);
        return null;
    }
   
}